{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import functools\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    class Hero:\n",
    "        DEFAULT_NAME = \"Superman\"\n",
    "        def __init__(self):\n",
    "            self.name = self.DEFAULT_NAME\n",
    "\n",
    "        def rename(self, new_name):\n",
    "            self.name = new_name\n",
    "\n",
    "        reset_name = functools.partial(rename, DEFAULT_NAME)\n",
    "\n",
    "        def __repr__(self):\n",
    "            return f\"Hero({self.name!r})\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "rename() missing 1 required positional argument: 'new_name'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-3b1898c093f2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      4\u001b[0m     \u001b[0mhero\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrename\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Batman\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m     \u001b[0;32massert\u001b[0m \u001b[0mhero\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"Batman\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m     \u001b[0mhero\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreset_name\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      7\u001b[0m     \u001b[0;32massert\u001b[0m \u001b[0mhero\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"Batman\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: rename() missing 1 required positional argument: 'new_name'"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    hero = Hero()\n",
    "    assert hero.name == \"Superman\"\n",
    "    hero.rename(\"Batman\")\n",
    "    assert hero.name == \"Batman\"\n",
    "    hero.reset_name()\n",
    "    assert hero.name == \"Batman\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Solution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import functools\n",
    "\n",
    "class Hero:\n",
    "    DEFAULT_NAME = \"Superman\"\n",
    "    def __init__(self):\n",
    "        self.name = self.DEFAULT_NAME\n",
    "    \n",
    "    def rename(self, new_name):\n",
    "        self.name = new_name\n",
    "        \n",
    "    reset_name = functools.partialmethod(rename, DEFAULT_NAME)\n",
    "        \n",
    "    def __repr__(self):\n",
    "        return f\"Hero({self.name!r})\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "hero = Hero()\n",
    "assert hero.name == \"Superman\"\n",
    "hero.rename(\"Batman\")\n",
    "assert hero.name == \"Batman\"\n",
    "hero.reset_name()\n",
    "assert hero.name == \"Superman\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
